//
//  SetCardViewController.m
//  PokerHand
//
//  Created by mrbradle on 10/12/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "SetCardViewController.h"

@implementation SetCardViewController

@synthesize acceptableLabel, button, card, hand, suitControl, valueControl, valueLabel;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

//Override it so it can be initialized with a card
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil card:(Card*) theCard button:(UIButton*) theButton hand:(PokerHand*) theHand;
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        card = theCard;
        button = theButton;
        hand = theHand;
    }
    return self;
}

- (IBAction)suitSet {
    
    if (![self verifyCard]) {
        acceptableLabel.text = @"That card has been used.  Please pick another card.";
        return;
    }
    acceptableLabel.text = @"";
    card.suit = suitControl.selectedSegmentIndex;
    card.value = round(valueControl.value);
    [self updateImage];
    
}

- (void)updateImage {
    [button setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%d",card.suit+(12-card.value)*4+1]] forState:UIControlStateNormal];
}

- (IBAction)valueSet {
    
    int temp = card.value;
    card.value = round(valueControl.value);
    [self updateLabel];
    if (![self verifyCard]) {
        acceptableLabel.text = @"That card has been used.  Please pick another card.";
        card.value = temp;
        return;
    }
    card.suit = suitControl.selectedSegmentIndex;
    [self updateImage];
    
}

- (bool)verifyCard {
    
    for (int i = 0; i < 5; i++) {
        Card* testCard = [hand.cards objectAtIndex:i];
        if (testCard.value == round(valueControl.value) && testCard.suit == suitControl.selectedSegmentIndex
            && testCard != card) return NO;
    }
    acceptableLabel.text = @"";
    return YES;
    
}

- (void)updateLabel {
    NSString* temp;
    switch (card.value) {
        case 9:
            temp = @"Jack";
            break;
        case 10:
            temp = @"Queen";
            break;
        case 11:
            temp = @"King";
            break;
        case 12:
            temp = @"Ace";
            break;
        default:
            temp = [NSString stringWithFormat:@"%d",(card.value+2)];
            break;
    }
    
    valueLabel.text = temp;
    //[temp release];
}

- (void)dealloc
{
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    //suit = card.suit;
    //value = card.value;
    
    [valueControl setValue:card.value animated:NO];
    [self updateLabel];
    //[suitControl setEnabled:YES forSegmentAtIndex:card.suit];
    suitControl.selectedSegmentIndex = card.suit;
    
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
